package com.kun.controller;


import com.kun.common.Constants;
import com.kun.common.Result;
import com.kun.controller.dto.CourseDto;
import com.kun.entity.Attachment;
import com.kun.entity.Course;
import com.kun.exception.MyServiceException;
import com.kun.service.CourseServiceImpl;
import com.kun.service.UserServiceImpl;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/course")
public class CourseController {

    @Autowired
    private CourseServiceImpl courseService;

    @Autowired
    private UserServiceImpl userService;

    @GetMapping("/queryCourse")
    @ApiOperation("根据教师id查询课程")
    @ResponseBody
    public Result queryCourse(@RequestParam("id")int teacherId){
        HashMap<String, Object> map = new HashMap<>();
        map.put("teacherId",teacherId);
        List<Course> courses = courseService.selectAllCourses(map);
        return  Result.success(courses);
    }


    @GetMapping("/deleteCourse")
    @ApiOperation("删除课程")
    @ResponseBody
    public Result deleteCourse(@RequestParam("id")int id){
        courseService.deleteCourse(id);
        return  Result.success();
    }

    @PostMapping("/addCourse")
    @ApiOperation("新增课程")
    @ResponseBody
    public Result addCourse(@RequestBody Course course){
        courseService.addCourse(course);
        return  Result.success();
    }

    @PostMapping("/batchDeleteCourse")
    @ApiOperation("批量删除课程")
    @ResponseBody
    public Result batchDeleteCourse(@RequestBody List<Integer> ids){
        for (Integer id : ids) {
            this.deleteCourse(id);
        }
        return  Result.success();
    }


    @GetMapping("/queryAllCourses")
    @ApiOperation("查询所有的课程")
    @ResponseBody
    public Result queryAllCourses(@ApiParam("当前页数") @RequestParam("pageIndex")int pageIndex,
                                @ApiParam("页面大小") @RequestParam("pageSize") int pageSize,
                                @ApiParam("课程名") @RequestParam(value = "courseName",required = false) String courseName){
        HashMap<String, Object> map = new HashMap<>();
        map.put("pageIndex",pageIndex);
        map.put("pageSize",pageSize);
        map.put("courseName_like",courseName);
        List<Course> courses = courseService.selectAllCourses(map);
        ArrayList<CourseDto> list = new ArrayList<>();
        for (Course course : courses) {
            CourseDto courseDto = new CourseDto();
            courseDto.setCourse(course, userService.selectUserById(course.getTeacherId()).getNickname());
            list.add(courseDto);
        }
        return  Result.success(list);
    }

    @GetMapping("/queryAllCoursesNum")
    @ApiOperation("查询所有的课程数目")
    @ResponseBody
    public Result queryAllCoursesNum(@ApiParam("课程名") @RequestParam(value = "courseName",required = false) String courseName){
        HashMap<String, Object> map = new HashMap<>();
        map.put("courseName_like",courseName);
        List<Course> courses = courseService.selectAllCourses(map);
        return  Result.success(courses.size());
    }

    @GetMapping("/editCourseState")
    @ApiOperation("编辑课程开课信息")
    @ResponseBody
    public Result editCourseState(@RequestParam("id") Integer id){

        HashMap<String, Integer> map = new HashMap<>();
        map.put("id",id);
        List<Course> courses = courseService.selectAllCourses(map);
        if (courses.size()>1){
            throw new MyServiceException(Constants.CODE_600,"数据库包含重复id的文件");
        }else{
            courses.get(0).setState(!courses.get(0).getState());
            HashMap<String, Object> hashMap = new HashMap<>();
            courseService.updateCourse(courses.get(0),hashMap);
            return  Result.success();
        }

    }



}
